#!/bin/bash

# 快速验证服务器HTTP/2修复状态
echo "🔍 SPAM3UK HTTP/2修复状态检查"
echo "========================================"

# 检查代码修复状态
echo "1️⃣ 检查本地代码修复状态..."
echo "----------------------------------------"

if grep -q "ForceAttemptHTTP2.*false" internal/scraper/vehicle_scraper.go; then
    echo "  ✅ HTTP/2禁用配置: 存在"
else
    echo "  ❌ HTTP/2禁用配置: 缺失"
fi

if grep -q "req.Proto.*HTTP/1.1" internal/scraper/vehicle_scraper.go; then
    echo "  ✅ HTTP/1.1强制: 存在"
else
    echo "  ❌ HTTP/1.1强制: 缺失"
fi

if grep -q '"host": "0.0.0.0"' spam3uk-config.json; then
    echo "  ✅ 服务器绑定: 0.0.0.0"
else
    echo "  ❌ 服务器绑定: localhost"
fi

echo ""
echo "2️⃣ 检查服务器运行状态..."
echo "----------------------------------------"

# 测试服务器健康状态
if curl -s http://101.201.32.200:5700/health > /dev/null; then
    echo "  ✅ 服务器健康检查: 正常"
    curl -s http://101.201.32.200:5700/health | jq .
else
    echo "  ❌ 服务器健康检查: 失败"
fi

echo ""
echo "3️⃣ 测试抓取功能..."
echo "----------------------------------------"
echo "  🧪 执行抓取测试 (10秒超时)..."

START_TIME=$(date +%s)
RESPONSE=$(timeout 10 curl -s -X POST http://101.201.32.200:5700/api/scrape_all 2>/dev/null)
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))

if echo "$RESPONSE" | grep -q '"code":200'; then
    echo "  ✅ 抓取功能: 正常 (${DURATION}s)"
    echo "  📊 抓取结果:"
    echo "$RESPONSE" | jq .
elif echo "$RESPONSE" | grep -q "stream error"; then
    echo "  ❌ 抓取功能: HTTP/2错误仍存在"
    echo "  🚨 需要在服务器部署最新代码！"
else
    echo "  ⚠️ 抓取功能: 其他错误"
    echo "  📝 响应: $RESPONSE"
fi

echo ""
echo "📋 修复建议:"
echo "========================================"
if echo "$RESPONSE" | grep -q "stream error"; then
    echo "🚨 服务器代码版本过旧，需要部署最新修复:"
    echo ""
    echo "在服务器执行以下命令:"
    echo "1. git pull origin master"
    echo "2. go build -o spam3uk main.go"
    echo "3. pkill -f spam3uk && nohup ./spam3uk > server.log 2>&1 &"
    echo ""
    echo "或使用部署脚本: ./deploy_server.sh"
else
    echo "✅ 服务器代码已是最新版本"
fi
